package com.codegenerate.demo.config;

import com.baomidou.mybatisplus.extension.api.R;
import com.codegenerate.demo.exceptions.ValidateBusinessException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理自定义的业务异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = ValidateBusinessException.class)
    @ResponseBody
    public R bizExceptionHandler(HttpServletRequest req, ValidateBusinessException e){
        return R.failed(e.getMsg());
    }

    /**
     * 处理空指针的异常
     * @param req
     * @param e
     * @return
     */
   /* @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public R exceptionHandler(HttpServletRequest req, NullPointerException e){
        System.out.println(e.getMessage());
        return R.failed("空指针:"+e.getMessage());
    }*/


//    /**
//     * 处理其他异常
//     * @param req
//     * @param e
//     * @return
//     */
//    @ExceptionHandler(value =Exception.class)
//    @ResponseBody
//    public R exceptionHandler(HttpServletRequest req, Exception e){
//        return R.failed(e.getMessage());
//    }
}
